package com.bianguo.android.baidumapdemo.utils;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/3/6.
 */

public class PhoneUtil {

    public static final String SEND_INTENT = "ACTION_SMS_SEND";
    public static final String DELIVER_INTENT = "ACTION_SMS_DELIVER";

    /**
     * 发送短信
     * @param mContext
     * @param phoneNumber
     * @param message
     */
    public static void sendSms(Context mContext, String phoneNumber, String message) {
        //获取短信管理器
        android.telephony.SmsManager smsManager = android.telephony.SmsManager.getDefault();
        PendingIntent sentPI = PendingIntent.getBroadcast(mContext, 0, new Intent("ACTION_SMS_SEND"), 0);
        PendingIntent deliverPI = PendingIntent.getBroadcast(mContext, 0, new Intent("ACTION_SMS_DELIVER"), 0);
        //拆分短信内容（手机短信长度限制）
        ArrayList<String> divideContents = smsManager.divideMessage(message);
        for (String text : divideContents) {
            smsManager.sendTextMessage(phoneNumber, null, text, sentPI, deliverPI);
        }
    }

    /**
     * 判断apk是否安装
     * @param mContext
     * @param packageName
     * @return
     */
    public static boolean checkAppExist(Context mContext, String packageName) {
        if (TextUtils.isEmpty(packageName)) {
            return false;
        }
        List<PackageInfo> installedPackages = mContext.getPackageManager().getInstalledPackages(0);
        if (installedPackages != null) {
            for (PackageInfo packageInfo : installedPackages) {
                if (packageName.equals(packageInfo.packageName)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 获取状态栏高度
     * @param context
     * @return
     */
    public static int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
}
